var util = require("../../../utils/util.js"),
    {
        api
    } = require("../../../utils/api.js"), 
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageSize: 10,
        page: 1,
        list: [],
        proScroll: false,
        showEmpty: false,
        userInfo: null,
        totalMoney: 0,
        show: false,
        playType: 1,
        goods_type: 1,
        activeNum: 0
    },
    close() {
        this.setData({
            show: false
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.hideShareMenu()
        this.setData({
            active: Number(options.type)
        });
        if (options.type == 0) {
            this.onChange();
        }
        this.getUsers();
    },
    async getUsers() {
        const data = await app.getUsers();
        data && this.setData(data)
    },
    onChange(e) {
        this.setData({
            page: 1,
            activeNum: e ? e.detail.name : 0
        });
        this.getOrderList();
    },
    async getOrderList() {
        let {
            data: {
                list
            }
        } = await api.getOrderList({
            page: this.data.page,
            pageSize: this.data.pageSize,
            status: this.data.activeNum == -2?'':this.data.activeNum
        });
        let listArr = list && Array.isArray(list.data) ? list.data : [];
        this.setData({
            proScroll: listArr.length < this.data.pageSize ? true : false,
            list: this.data.page == 1 ? listArr : [...this.data.list, ...listArr],
            showEmpty: list.total == 0 ? true : false
        })
    },
    // 未支付时取消订单 
    async cancelOrder(e) {
        const order_no = e.currentTarget.dataset.orderno;
        wx.showModal({
            title: '提示',
            content: '是否要取消此订单',
            complete: async (res) => {
                if (res.confirm) {
                    await api.cancelOrder({order_no});
                    app.showError('操作成功');
                    this.setData({page:1})
                    this.getOrderList();
                }
            }
        })
    },
    hxOrder(e) {
        const {
            orderno
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/mine/orderhx/orderhx?order_no=' + orderno,
        })
    },
    // 删除订单 
    async delOrder(e) {
        const order_no = e.currentTarget.dataset.orderno;
        wx.showModal({
            title: '提示',
            content: '是否要取消此订单',
            complete: async (res) => {
                if (res.confirm) {
                    let {
                        code
                    } = await api.delOrder({
                        order_no
                    });
                    app.showError('操作成功')
                }
            }
        })
    },
    againPlay(e) {
        const {
            orderno,
            type,
            price
        } = e.currentTarget.dataset;
        if (type == 0) {
            this.setData({
                // show: true,
                order_no: orderno,
                playType: '1',
                totalMoney: price
            })
            this.againPalyOrder();
        } else {
            if(this.data.disabled)return;
            this.setData({disabled: true})
            this.pointCartPlay()
        }
    },
    againPalyOrder() {
        if(this.data.disabled)return;
        this.setData({disabled: true})
        const {playType,order_no}=this.data;
        this.close();
        if(playType == 1){
            this.moneyWxPlay(order_no)
        }else{
            this.pointCartPlay()
        }
    },
    // 金额支付
    moneyWxPlay(order_no){
        api.againPalyOrder({order_no})
        .then(play=>{
            let payment=play.data && play.data.payment && play.data.payment.paySign?play.data.payment:null;
            if(!payment){
                this.setData({disabled: false})
                return wx.showToast({icon: 'none',title: '支付参数错误'});
            }
            return app.moneyWxPlay(payment)
        })
        .then(result=>this.playResult())
        .catch(err=>{
            this.setData({disabled:false})
        })
    },
    // 积分或支付
    pointCartPlay(){
        this.playResult('兑换成功！');
    },
    chcard(){
        wx.navigateTo({
          url: '/pages/mine/coupons/coupons',
        })
    },
    playResult(msg='支付成功！') {
        app.showError(msg)
        this.setData({page: 1,disabled:false})
        this.getOrderList();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (!this.data.proScroll) {
            let page = this.data.page;
            page = page + 1;
            this.setData({
                page: page
            })
            this.getOrderList();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {

    }
})